package com.anolesoft.amljmes.fourteenDispatch.controller;

import com.anolesoft.amljmes.fourteenDispatch.dto.MesWpgProcessDto;
import com.anolesoft.amljmes.fourteenDispatch.service.info.MesWpgProcessService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/wxpggxwh")
public class MesWpgProcessController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String gongxuName;
        private String factoryId;
    }

    @Autowired
    public MesWpgProcessService mesWpgProcessService;

    //查询
    @PostMapping(value="/query",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryWpgProcessInfo(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<MesWpgProcessDto> allList = mesWpgProcessService.queryWpgProcessInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesWpgProcessDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //新增
    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult addMesWpgProcess(@RequestBody MesWpgProcessDto inFoDtoParam){
        System.out.println(">>>>>>add");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        inFoDtoParam.setCjr(loginUser.getEmpUid());
        inFoDtoParam.setFactoryId(loginUser.getFacCode());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesWpgProcessService.addMesWpgProcess(inFoDtoParam));
    }

    //修改
    @PostMapping(value="/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateMesWpgProcess(@RequestBody MesWpgProcessDto inFoDtoParam){
        System.out.println(">>>>>>update");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        inFoDtoParam.setXgr(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesWpgProcessService.updateMesWpgProcess(inFoDtoParam));
    }
    //删除
    @PostMapping(value="/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult deleteMesWpgProcess(@RequestBody MesWpgProcessDto inFoDtoParam){
        System.out.println(">>>>>>delete");
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesWpgProcessService.deleteMesWpgProcess(inFoDtoParam));
    }
    // 批量删除
    @PostMapping(value="/deletes", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult delete(@RequestBody String[] objuids){
        System.out.println(">>>>>>delete");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesWpgProcessService.delete(asList));
    }

    // 验证名称重复
    @PostMapping(value="/gongxuNameRules", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult isMesWpgProcessName(@RequestBody MesWpgProcessDto inFoDtoPara){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        inFoDtoPara.setCjr(loginUser.getEmpUid());
        inFoDtoPara.setFactoryId(loginUser.getFacCode());

        String gongxuName = "";
        int count = mesWpgProcessService.isMesWpgProcessName(inFoDtoPara);
        if(count > 0){
            gongxuName = "名称已存在！";
        }
        return new AjaxResult(HttpStatus.SUCCESS, gongxuName);
    }

    // 查询无需派工工序名
    @PostMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult categoryAll(){
        List<MesWpgProcessDto> mesWpgProcessDtos = mesWpgProcessService.gongxuNameAll();
        AjaxResult success = AjaxResult.success();
        success.put("data", mesWpgProcessDtos);
        return success;
    }
}
